/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.web.merchant;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.utils.StringUtils;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.ivm.entity.order.Prompt;
import com.neusoft.lohas.modules.ivm.service.order.PromptService;

/**
 * 温馨提示Controller
 * @author Jackie.Liu
 * @version 2015-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/ivm/order/prompt")
public class PromptController extends BaseController {

	@Autowired
	private PromptService promptService;
	
	@ModelAttribute
	public Prompt get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return promptService.get(id);
		}else{
			return new Prompt();
		}
	}
	
	@RequiresPermissions("ivm:order:prompt:view")
	@RequestMapping(value = "form")
	public String form(Prompt prompt, Model model) {
		if (StringUtils.isBlank(prompt.getId())){
			prompt.setMerchant(super.merchant);
			prompt = promptService.findByMerchantId(prompt.getMerchant().getId());
		}
		model.addAttribute("prompt", prompt);
		return "modules/ivm/order/promptForm";
	}

	@RequiresPermissions("ivm:order:prompt:edit")
	@RequestMapping(value = "save")
	public String save(Prompt prompt, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, prompt)){
			return form(prompt, model);
		}
		promptService.save(prompt);
		addMessage(redirectAttributes, "保存温馨提示成功");
		return "redirect:"+Global.getAdminPath()+"/ivm/order/prompt/form";
	}
	
}
